package com.leo.lime.music.content.listener.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.leo.lime.music.R;
import com.leo.lime.music.content.listener.control.ListenerControl;
import com.leo.lime.music.content.listener.utils.ListenerUtils;
import com.leo.lime.music.view.DrawableTextView;
import com.leo.lime.music.view.LimeImageSwitcher;

/**
 * Created by android on 18-8-15.
 */

public class ListenerFragment extends Fragment {
    private static final String TAG = "ListenerFragment";

    private ListenerControl mListenerControl;
    private int mCurrentImagePosition;

    private LimeImageSwitcher mListenerImageSwitcher;

    private DrawableTextView mMusicDbDrawableTV;
    private DrawableTextView mSongListDrawableTV;
    private DrawableTextView mRadioDrawableTV;
    private DrawableTextView mGuessLikeDrawableTV;
    private DrawableTextView mDailyRecommendDrawableTV;
    private DrawableTextView mMoreDrawableTV;

    private TextView mRecommendNewMusicTV;
    private TextView mRecommendLiveTV;
    private TextView mRecommendMusicListTV;
    private TextView mRecommendAdvisoryTV;
    private TextView mRecommendVideoTV;

    private ViewSwitcher.ViewFactory mImageViewFactory = new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            ImageView iv = new ImageView(getContext());
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            // int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
            int height = getActivity().getWindowManager().getDefaultDisplay().getHeight();
            iv.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height / 4));
            return iv;
        }
    };

    private View.OnTouchListener mListenerISTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            float downX = 0;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    float upX = event.getX();
                    int position = getCurrentImagePosition();
                    int length = mListenerControl.getListenerImages().length;
                    if(upX < downX) {
                        if(position > 0 && position < length) {
                            setCurrentImagePosition(--position);
                        } else {
                            setCurrentImagePosition(length - 1);
                        }
                    } else {
                        if(position >= 0 && position < length - 1) {
                            setCurrentImagePosition(++position);
                        } else {
                            setCurrentImagePosition(0);
                        }
                    }
                    mListenerImageSwitcher.setImageResource(getCurrentImagePosition());
                    break;
            }
            return false;
        }
    };

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            }
        }
    };

    private View.OnClickListener mOnFunctionClickListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.listener_music_db:
                    onMusicDbFuncClick(view);
                    break;
                case R.id.listener_music_list:
                    onMusicListFuncClick(view);
                    break;
                case R.id.listener_radio:
                    onRadioFuncClick(view);
                    break;
                case R.id.listener_guess_like:
                    onGuessLikeFuncClick(view);
                    break;
                case R.id.listener_daily_recommend:
                    onDailyRecommendFuncClick(view);
                    break;
                case R.id.listener_more:
                    onMoreFuncClick(view);
                    break;
            }
        }
    };

    private View.OnClickListener mOnRecommendViewClickListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.listener_recommend_music_new:
                    onRecommendNewMusicClick(view);
                    break;
                case R.id.listener_recommend_live:
                    onRecommendLiveClick(view);
                    break;
                case R.id.listener_recommend_music_list:
                    onRecommendMusicListClick(view);
                    break;
                case R.id.listener_recommend_advisory:
                    onRecommendAdvisoryClick(view);
                    break;
                case R.id.listener_recommend_video:
                    onRecommendVideoClick(view);
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListenerControl = new ListenerControl(getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.content_listener_view, null, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initialImageSwitcher(view);
        initialFunctions(view);
        initialRecommendView(view);
    }

    private void initialImageSwitcher(View view) {
        mListenerImageSwitcher = view.findViewById(R.id.listener_is);
        mListenerImageSwitcher.setFactory(mImageViewFactory);
        mListenerImageSwitcher.setOnTouchListener(mListenerISTouchListener);
        setCurrentListenerImage();
        mHandler.postDelayed(mAutoChangeImageSwitcherRunnable, ListenerUtils.DEFAULT_IMAGE_SWITCHER_DELAY);
    }

    private void initialFunctions(View view) {
        mMusicDbDrawableTV = view.findViewById(R.id.listener_music_db);
        mSongListDrawableTV = view.findViewById(R.id.listener_music_list);
        mRadioDrawableTV = view.findViewById(R.id.listener_radio);
        mGuessLikeDrawableTV = view.findViewById(R.id.listener_guess_like);
        mDailyRecommendDrawableTV = view.findViewById(R.id.listener_daily_recommend);
        mMoreDrawableTV = view.findViewById(R.id.listener_more);

        mMusicDbDrawableTV.setOnClickListener(mOnFunctionClickListener);
        mSongListDrawableTV.setOnClickListener(mOnFunctionClickListener);
        mRadioDrawableTV.setOnClickListener(mOnFunctionClickListener);
        mGuessLikeDrawableTV.setOnClickListener(mOnFunctionClickListener);
        mDailyRecommendDrawableTV.setOnClickListener(mOnFunctionClickListener);
        mMoreDrawableTV.setOnClickListener(mOnFunctionClickListener);
    }

    private void initialRecommendView(View view) {
        mRecommendNewMusicTV = view.findViewById(R.id.listener_recommend_music_new);
        mRecommendLiveTV = view.findViewById(R.id.listener_recommend_live);
        mRecommendMusicListTV = view.findViewById(R.id.listener_recommend_music_list);
        mRecommendAdvisoryTV = view.findViewById(R.id.listener_recommend_advisory);
        mRecommendVideoTV = view.findViewById(R.id.listener_recommend_video);

        mRecommendNewMusicTV.setOnClickListener(mOnRecommendViewClickListener);
        mRecommendLiveTV.setOnClickListener(mOnRecommendViewClickListener);
        mRecommendMusicListTV.setOnClickListener(mOnRecommendViewClickListener);
        mRecommendAdvisoryTV.setOnClickListener(mOnRecommendViewClickListener);
        mRecommendVideoTV.setOnClickListener(mOnRecommendViewClickListener);
    }

    private int getCurrentImagePosition() {
        return mCurrentImagePosition;
    }

    private void setCurrentImagePosition(int position) {
        mCurrentImagePosition = position;
    }

    private void setNextImagePosition() {
        int position = getCurrentImagePosition();
        int length = mListenerControl.getListenerImages().length;
        if(position >= 0 && position < length - 1) {
            setCurrentImagePosition(++position);
        } else {
            setCurrentImagePosition(0);
        }
    }

    private void setCurrentListenerImage() {
        mListenerImageSwitcher.setImageResource(mListenerControl.getListenerImages()[getCurrentImagePosition()]);
    }

    private Runnable mAutoChangeImageSwitcherRunnable = new Runnable() {
        @Override
        public void run() {
            setNextImagePosition();
            setCurrentListenerImage();
            mHandler.postDelayed(mAutoChangeImageSwitcherRunnable, ListenerUtils.DEFAULT_IMAGE_SWITCHER_DELAY);
        }
    };

    private void onMusicDbFuncClick(View view) {}

    private void onMusicListFuncClick(View view) {}

    private void onRadioFuncClick(View view) {}

    private void onGuessLikeFuncClick(View view) {}

    private void onDailyRecommendFuncClick(View view) {}

    private void onMoreFuncClick(View view) {}

    private void onRecommendNewMusicClick(View view) {}

    private void onRecommendLiveClick(View view) {}

    private void onRecommendMusicListClick(View view) {}

    private void onRecommendAdvisoryClick(View view) {}

    private void onRecommendVideoClick(View view) {}
}
